package com.matrix.basecore.base

import android.app.Application
import android.util.Log
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.matrix.basecore.constant.Constant
import com.matrix.basecore.constant.Constant.appInits
import org.litepal.LitePal
import org.litepal.LitePalApplication

class BaseApplication : LitePalApplication() {

    var app: Application = this

    override fun onCreate() {
        super.onCreate()
        LitePal.initialize(this)
        initARouter()
        initModuleHighPriority()
        initModuleLowPriority()
    }

    private fun initARouter() {
        if (Constant.isDebug) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(app)
    }

    private fun initModuleHighPriority() {
        for (item in appInits) {
            try {
                var clazz: Class<*> = Class.forName(item)
                var appInit: BaseAppInit = clazz.newInstance() as BaseAppInit
                appInit.onInitHighPriority(this)
            } catch (e: ClassNotFoundException) {
                e.printStackTrace()
            }
        }
    }

    private fun initModuleLowPriority() {
        for (item in appInits) {
            try {
                var clazz: Class<*> = Class.forName(item)
                var appInit: BaseAppInit = clazz.newInstance() as BaseAppInit
                appInit.onInitHighPriority(this)
            } catch (e: ClassNotFoundException) {
                e.printStackTrace()
            }
        }
    }
}